﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

namespace WebC
{
    public partial class upload : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnUpload.Attributes.Add("onclick", "return checkname();");
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string afterName = Path.GetExtension(flUpload.FileName);//扩展名
            string sName = DateTime.Now.ToString("yyyyMMddHHmm") + afterName; 
            string fileName = Request["filename"];//全名
            //文件根目录
            string BasePath = Convert.ToString(ConfigurationManager.AppSettings["FileUrl"]);
            string sFilePath = Convert.ToString(ConfigurationManager.AppSettings["FilePath"]);

            BasePath = sBaseUrl + BasePath + "/";
            sFilePath = Server.MapPath("~") + sFilePath + @"\" + fileName + @"\";

            try
            {
                if (!Directory.Exists(sFilePath))
                {
                    Directory.CreateDirectory(sFilePath);
                }
                flUpload.SaveAs(sFilePath + sName);
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "savsuc", "<script>alert('附件上传失败！');</script>");
                return;
            }
            string sImg = "";
            switch (afterName)
            {
                case ".rar":
                    sImg = "rar";
                    break;
                case ".xls":
                    sImg = ".xls";
                    break;
                case ".zip":
                    sImg = "rar";
                    break;
                case ".txt":
                    sImg = "txt";
                    break;
                case ".doc":
                    sImg = "doc";
                    break;
                default:
                    sImg = "un";
                    break;
            }
            string sHtml = string.Format(@"<b><a href=""{0}"" target=""_blank""><img src=""{3}images/{2}.jpg"" />{1}</a><input type=""button"" value=""删除"" onclick=""if(deleFile(\'{4}\')) this.parentNode.parentNode.removeChild(this.parentNode);"" />&nbsp;&nbsp;</b>",
                BasePath + fileName + "/" + sName, txtFileName.Text, sImg, sBaseUrl, sName);
            ClientScript.RegisterStartupScript(this.GetType(), "suc"
                , string.Format(@"<script language='javascript'>window.parent.document.getElementById('tdupload').innerHTML+='{0}';alert('文件上传成功！')</script>", sHtml));
            txtFileName.Text = "";
        } 
    }
}
